package seed.web.servlet;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import seed.domain.PageBean;
import seed.domain.Seed;
import seed.service.SeedService;


import category.domain.Category;
import category.service.CategoryService;
import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;


public class AdminSeedServlet extends BaseServlet {
	SeedService seedService = new SeedService();
	private CategoryService categoryService = new CategoryService();
	
	

	
	/**
	 * 添加种子
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addPre(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 查询所有分类，保存到request，转发到add.jsp
		 * add.jsp中把所有的分类使用下拉列表显示在表单中
		 */
		request.setAttribute("categoryList", categoryService.findAll());
		return "f:/adminjsps/admin/seed/add.jsp";
	}
	public String addPre1(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 查询所有分类，保存到request，转发到add.jsp
		 * add.jsp中把所有的分类使用下拉列表显示在表单中
		 */
		request.setAttribute("categoryList", categoryService.findAll());
		return "f:/addProduct.jsp";
	}

	public String load(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 1. 获取参数bid，通过bid调用service方法得到Book对象
		 * 2. 获取所有分类，保存到request域中
		 * 3. 保存book到request域中，转发到desc.jsp
		 */
		//request.setAttribute("book", boService.load(request.getParameter("bid")));
		request.setAttribute("categoryList", categoryService.findAll());
		return "f:/adminjsps/admin/book/desc.jsp";
	}
	
	/**
	 * 查看所有图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pc = getPc(request);//寰楀埌PC
    	int ps = 4;//姣忛〉4琛岃褰�
    	PageBean<Seed> pb = seedService.findAll(pc,ps);
    	request.setAttribute("pb", pb);
    	//request.setAttribute("seedList", seedService.findAll());
    	
		return "f:/adminjsps/admin/seed/list.jsp";
	}
	 public int getPc(HttpServletRequest request){
	    	String value = request.getParameter("pc");
	    	if(value == null || value.trim().isEmpty()){
	    		return 1;
	    	}
	    	return Integer.parseInt(value);
	    }
	/**
	 * 删除图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String sid = request.getParameter("sid");
		seedService.delete(sid);
		return findAll(request, response);
	}
	
	/**
	 * 修改图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Seed seed = CommonUtils.toBean(request.getParameterMap(), Seed.class);
		Category category = CommonUtils.toBean(request.getParameterMap(), Category.class);
		seed.setCategory(category);
		
		//seedService.edit(seed);
		return findAll(request, response);
	}
	/**
	 * 后台热点统计
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String overWatch(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pc = getPc(request);//寰楀埌PC
    	int ps = 10;//姣忛〉4琛岃褰�
    	PageBean<Seed> pb = seedService.findAll(pc,ps);
    	request.setAttribute("pb", pb);
    	//request.setAttribute("seedList", seedService.findAll());
    	
		return "f:/adminjsps/admin/seed/overWatch.jsp";
	}
}
